login_register = {
    forget: function ()
    {
        $('#forget_password').click(function ()
        {
            showdialog('忘记密码?那你还是直接来问我吧,懒得做了^_^')
        });
    },
    startanimate: function ()
    {
        $('#card-1').animate({
            width: 600,
            height: 500
        }, 2000);

        $('#card-2').animate({
            width: 600,
            height: 500
        }, 2000, function ()
        {
            $('#card-login').fadeIn(2000);
        });
    },
    login: function ()
    {
        login();
    },
    register: function ()
    {
        register();
    }
};
var isShow = false;
var isRegShow = false;
function showdialog(content)
{
    if (!isShow)
    {
        var dialog = $('#error_dialog');
        dialog.html(content);
        dialog.fadeIn(500);
        isShow = true;
        setTimeout(function ()
        {
            dialog.fadeOut(500);
            isShow = false;
        }, 3000);
    }
}

function showRegDialog(content)
{
    if (!isRegShow)
    {
        var dialog = $('#error_reg_dialog');
        dialog.html(content);
        dialog.fadeIn(500);
        isRegShow = true;
        setTimeout(function ()
        {
            dialog.fadeOut(500);
            isRegShow = false;
        }, 3000);
    }
}
var isProgressShwo = false;
/*登录*/
function login()
{
    $('#login_submit').click(function ()
    {
        if (!isProgressShwo)
        {
            var userName = $('#username');
            var userPassword = $('#userpassword');
            var remember = $('#remember');
            if (userName.val() != "")
                if (userPassword.val() != "")
                {
                    progress_show();
                    $.post("/login/submit",
                        {
                            userName: userName.val(),
                            userPassword: userPassword.val(),
                            remember: function ()
                            {
                                var b = remember.is(':checked');
                                if (!b)
                                {
                                    document.cookie = "ue=; expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
                                    document.cookie = "up=; expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/";
                                }
                                return b;
                            }
                        }, function (data, status)
                        {
                            if (status == 'success')
                            {
                                if (data.indexOf('登录成功') != -1 || data.indexOf('管理员') != -1)
                                {
                                    var ss = data.split("|");
                                    if (ss.length = 3)
                                    {
                                        var exdays = 30;
                                        var d = new Date();
                                        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
                                        var expires = "expires=" + d.toGMTString();

                                        document.cookie = "ue=" + ss[1] + "; expires=" + expires + "; path=/";
                                        document.cookie = "up=" + ss[2] + "; expires=" + expires + "; path=/";
                                    }
                                    /*如果有上一页则返回,否则跳转到首页*/
                                    if (data.indexOf('管理员') != -1)
                                        location.href = '/admin/index';
                                    else
                                    {
                                        document.referrer === '' ?
                                            location.href = '/' :
                                            self.location = document.referrer;
                                    }
                                }
                                else
                                    showdialog(data);
                            }
                        });
                }
                else
                    showdialog("密码为空!");
            else
                showdialog("账户为空!");
        }
    });
}

function progress_show()
{
    if (!isProgressShwo)
    {
        var progress_dialog = $('#login_progress');
        progress_dialog.fadeIn(1000);
        isProgressShwo = true;
        setTimeout(function ()
        {
            progress_dialog.fadeOut(1000);
            isProgressShwo = false;
        }, 3000);
    }
}


/*注册*/
function register()
{
    var userName = $('#register-username');
    var userPassword = $('#register-userpassword');
    var userPassword2 = $('#register-userpassword2');
    var userRealName = $('#register-userrealname');
    //var userFile = $('#register-userfile');
    $('#register-submit').click(function ()
    {
        if (userName.val() != "")
            if (userPassword.val() != "" && userPassword2.val() != "")
                if (userPassword.val() == userPassword2.val())
                    if (userRealName.val() != "")
                        if (userName.val().length >= 6)
                            if (userPassword.val().length >= 6)
                            {
                                $.post("/register/submit",
                                    {
                                        userName: userName.val(),
                                        userPassword: userPassword.val(),
                                        userRealName: userRealName.val()
                                    }, function (data, status)
                                    {
                                        result(data, status);
                                    });
                            }
                            else
                                showRegDialog("密码过短,不得小于6位!");
                        else
                            showRegDialog("账号过短,不得小于6位!");
                    else
                        showRegDialog("请填写您的真实姓名!");
                else
                    showRegDialog("两次密码输入不一致!");
            else
                showRegDialog("密码输入为空!");
        else
            showRegDialog("账号输入为空!");
    });
}
/*注册结果*/
function result(data, status)
{
    if (status == 'success')
    {
        var register = $('#register');
        if (data == '注册成功')
        {
            $('#success_reg_dialog').fadeIn(1000);
            register.modal('show');
            $('#username').val($('#register-username').val());
            $('#userpassword').val($('#register-userpassword').val());
            setTimeout(function ()
            {
                register.modal('hide');
            }, 3000);
        }
        else
            showRegDialog(data);
    }
    else
        showRegDialog('连接服务器失败!');
}